package com.example.charge;

import android.animation.ValueAnimator;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import java.text.DecimalFormat;


public class MainActivity extends AppCompatActivity {

    private TextView tvWallet;
    private BatteryMonitor batteryMonitor;
    private double balance = 500.00;  // 初始余额
    private boolean isCharging = false;  // 是否正在充电
    private DecimalFormat df = new DecimalFormat("0.00");  // 格式化金额
    private final Handler handler = new Handler(Looper.getMainLooper());
    private ValueAnimator currentAnimator;  // 用于管理当前动画

    // 每秒增加的金额
    private static final double INCREMENT_PER_SECOND = 5.0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initViews();
        initData();
        initBatteryMonitor();
    }

    private void initViews() {
        tvWallet = findViewById(R.id.tv_wallet);
    }

    private void initData() {
        updateBalanceDisplay();
    }

    private void initBatteryMonitor() {
        batteryMonitor = new BatteryMonitor(this);
        batteryMonitor.setOnChargeStateChangeListener(new BatteryMonitor.OnChargeStateChangeListener() {
            @Override
            public void onChargeStarted() {
                isCharging = true;
                startBalanceAnimation();
            }

            @Override
            public void onChargeStopped() {
                isCharging = false;
                if (currentAnimator != null) {
                    currentAnimator.cancel();
                }
            }
        });
    }

    // 启动余额平滑动画
    private void startBalanceAnimation() {
        // 动画从当前余额开始，到“当前余额 + 每秒增量”结束，时长1秒
        final double start = balance;
        final double end = balance + INCREMENT_PER_SECOND;

        currentAnimator = ValueAnimator.ofFloat((float) start, (float) end);
        currentAnimator.setDuration(1000);  // 动画时长1秒
        currentAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float animatedValue = (float) animation.getAnimatedValue();
                balance = animatedValue;
                updateBalanceDisplay();
            }
        });
        currentAnimator.addListener(new android.animation.Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(android.animation.Animator animation) {
            }

            @Override
            public void onAnimationEnd(android.animation.Animator animation) {
                // 动画结束后，若仍在充电，继续启动下一轮动画
                if (isCharging) {
                    startBalanceAnimation();
                }
            }

            @Override
            public void onAnimationCancel(android.animation.Animator animation) {
            }

            @Override
            public void onAnimationRepeat(android.animation.Animator animation) {
            }
        });
        currentAnimator.start();
    }

    // 更新余额显示
    private void updateBalanceDisplay() {
        tvWallet.setText("¥ " + df.format(balance));
    }

    @Override
    protected void onResume() {
        super.onResume();
        batteryMonitor.startMonitoring();
    }

    @Override
    protected void onPause() {
        super.onPause();
        batteryMonitor.stopMonitoring();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        isCharging = false;
        if (currentAnimator != null) {
            currentAnimator.cancel();
        }
        batteryMonitor.stopMonitoring();
    }
}